package com.mixsmart.web.plugins.ztree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;

import com.mixsmart.commonweb.IWebConstant;
import com.mixsmart.commonweb.bean.entity.TCRoleUrlRes;
import com.mixsmart.commonweb.service.impl.RoleUrlResService;
import com.mixsmart.commonweb.utils.SpringContextUtils;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.core.service.IResQueryService;
import com.mixsmart.spellsql.utils.Cnd;
import com.mixsmart.spellsql.utils.Exp;
import com.mixsmart.utils.CollectionUtils;
import com.mixsmart.utils.StringUtils;
import com.mixsmart.web.plugins.bean.ZTreeData;

/**
 * URL资源与操作标识符组合zTree树
 * @author lmq
 * @version 1.0
 * @since 1.0
 * <br />
 * 2016年7月3日
 */
public class UrlResOpFlag2zTree extends AbsObj2zTree {

	
	
	private IResQueryService resQueryServ;
	
	private RoleUrlResService roleUrlResServ;
	
	private String roleId;
	
	public UrlResOpFlag2zTree() {
		resQueryServ = SpringContextUtils.find(IResQueryService.class);
		roleUrlResServ = SpringContextUtils.find(RoleUrlResService.class);
	}

	@Override
	protected void convert(ZTreeData zTreeData, Object[] objTmpArray) {
		String value = StringUtils.handNull(objTmpArray[objTmpArray.length-1]);
		if(StringUtils.isNotEmpty(value)) {
			zTreeData.setChecked(true);
			if(StringUtils.isEmpty(roleId)) {
				roleId = value;
			}
		}
	}

	@Override
	protected void afterConvert(List<ZTreeData> zTreeDatas) {
		if(CollectionUtils.isNotEmpty(zTreeDatas)) {
			List<ZTreeData> subZTreeDatas = new ArrayList<ZTreeData>();
			int perSize = 50;
			int totals = (int)Math.ceil(zTreeDatas.size()/(double)perSize);
			int times = 1;
			while(times <= totals) {
				String[] urlResIds = getUrlResIds(zTreeDatas, perSize, (times-1)*perSize);
				List<ZTreeData> datas = getAuthOpFlag(urlResIds, roleId);
				if(CollectionUtils.isNotEmpty(datas)) {
					subZTreeDatas.addAll(datas);
				}
				times++;
			}
			if(CollectionUtils.isNotEmpty(subZTreeDatas)) {
				zTreeDatas.addAll(subZTreeDatas);
			}
		}
	}
	
	/**
	 * 获取URL资源ID
	 * @param zTreeDatas
	 * @param perSize
	 * @param start
	 * @return
	 */
	private String[] getUrlResIds(List<ZTreeData> zTreeDatas, int perSize, int start) {
		int index = 0;
		int size = zTreeDatas.size()>perSize?perSize:zTreeDatas.size();
		String[] urlResIds = new String[size];
		for (int i = start; i < zTreeDatas.size(); i++) {
			zTreeDatas.get(i).setFlag(IWebConstant.RES_FLAG);
			if(index >= perSize) {
				break;
			}
			urlResIds[index] = zTreeDatas.get(i).getId();
			index++;
		}
		return urlResIds;
	}
	
	/**
	 * 获取权限操作符
	 * @param urlResIds URL资源ID数组
	 * @param roleId 角色ID
	 * @return 返回ZTreeData列表
	 */
	private List<ZTreeData> getAuthOpFlag(String[] urlResIds, String roleId) {
		List<ZTreeData> zTreeDatas = null;
		roleId = StringUtils.isEmpty(roleId)?"0":roleId;
		if(ArrayUtils.isNotEmpty(urlResIds)) {
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("roleId", roleId);
			params.put("urlResIds", urlResIds);
			MixResponse<Object> mixResp = resQueryServ.getDatas("urlRes_OpFlag_config_tree", params);
			List<TCRoleUrlRes> roleUrlResList = roleUrlResServ.finds(Cnd.where(Exp.in("urlResId", urlResIds)));
			if(mixResp.getResult().equals(IWebConstant.OP_SUCCESS)) {
				zTreeDatas = new ArrayList<ZTreeData>();
				TCRoleUrlRes roleUrlRes = null;
				String urlResId = null;
				for (Object tmp : mixResp.getDatas()) {
					Object[] objs = (Object[])tmp;
					ZTreeData zTreeData = new ZTreeData();
					zTreeData.setId(objs[0].toString());
					zTreeData.setpId(objs[2].toString());
					zTreeData.setName(objs[4].toString());
					zTreeData.setFlag(IWebConstant.AUTH_FLAG);
					if(!objs[2].toString().equals(urlResId)) {
						roleUrlRes = getRoleUrlRes(roleUrlResList, objs[2].toString());
						urlResId = objs[2].toString();
					}
					if(null != roleUrlRes && roleUrlRes.isContains(objs[1].toString())) {
						zTreeData.setChecked(true);
					} else {
						zTreeData.setChecked(false);
					}
					zTreeDatas.add(zTreeData);
				}
			}//if
		}
		return zTreeDatas;
	}
	
	/**
	 * 获取角色与URL资源关联的信息
	 * @param roleUrlResList 角色URL资源列表
	 * @param urlResId URL资源ID
	 * @return 返回角色URL资源对象
	 */
	private TCRoleUrlRes getRoleUrlRes(List<TCRoleUrlRes> roleUrlResList, String urlResId) {
		TCRoleUrlRes urlRes = null;
		if(CollectionUtils.isNotEmpty(roleUrlResList)) {
			for(TCRoleUrlRes urlResTmp : roleUrlResList) {
				if(urlResTmp.getUrlResId().equals(urlResId)) {
					urlRes = urlResTmp;
					break;
				}
			}
		}
		return urlRes;
	}
	
}
